import Web3 from 'web3'

const MULTISENDER_ADDRESS_FOUNDATION = process.env.REACT_APP_MULTISENDER_ADDRESS_FOUNDATION;
const MULTISENDER_ADDRESS_ROPSTEN = process.env.REACT_APP_MULTISENDER_ADDRESS_ROPSTEN;
const MULTISENDER_ADDRESS_GOERLI = process.env.REACT_APP_MULTISENDER_ADDRESS_GOERLI;
const MULTISENDER_ADDRESS_XDAI = process.env.REACT_APP_MULTISENDER_ADDRESS_XDAI;

const getWeb3 = () =>
    new Promise((resolve, reject) => {
      // Wait for loading completion to avoid race conditions with web3 injection timing.
      window.addEventListener("load", async () => {
        console.log("event listener start");
        // Modern dapp browsers...
        let results;
        if (window.ethereum) {
          const web3 = new Web3(window.ethereum);
          try {
            // Request account access if needed
            await window.ethereum.enable();
            // Acccounts now exposed
            let netId = await web3.eth.net.getId()
            let netIdName, trustApiName, explorerUrl, contractAddress;
            switch (netId) {
              case 1:
                netIdName = 'Foundation'
                trustApiName = 'api'
                explorerUrl = 'https://etherscan.io'
                contractAddress = MULTISENDER_ADDRESS_FOUNDATION
                console.log('This is Foundation', netId)
                break;
              case 3:
                netIdName = 'Ropsten'
                trustApiName = 'ropsten'
                explorerUrl = 'https://ropsten.etherscan.io'
                contractAddress = MULTISENDER_ADDRESS_ROPSTEN
                console.log('This is Ropsten', netId)
                break;
              case 4:
                netIdName = 'Rinkeby'
                trustApiName = 'rinkeby'
                explorerUrl = 'https://rinkeby.etherscan.io'
                console.log('This is Rinkeby', netId)
                break;
              case 5:
                netIdName = 'Goerli'
                trustApiName = 'goerli'
                explorerUrl = 'https://goerli.etherscan.io'
                console.log('This is Goerli', netId)
                contractAddress = MULTISENDER_ADDRESS_GOERLI
                break;
              case 42:
                netIdName = 'Kovan'
                trustApiName = 'kovan'
                explorerUrl = 'https://kovan.etherscan.io'
                console.log('This is Kovan', netId)
                break;
              case 99:
                netIdName = 'POA Core'
                trustApiName = 'poa'
                explorerUrl = 'https://poaexplorer.com'
                console.log('This is Core', netId)
                break;
              case 100:
                netIdName = 'xDai'
                trustApiName = 'xdai'
                explorerUrl = 'https://blockscout.com/xdai/mainnet'
                contractAddress = MULTISENDER_ADDRESS_XDAI
                console.log('This is xDai ', netId)
                break;
              case 77:
                netIdName = 'POA Sokol'
                trustApiName = 'https://trust-sokol.herokuapp.com'
                explorerUrl = 'https://sokol.poaexplorer.com'
                console.log('This is Sokol', netId)
                break;
              default:
                netIdName = 'Unknown'
                console.log('This is an unknown network.', netId)
            }
            document.title = `${netIdName} - BatchSender dApp`
            let account = await web3.eth.getAccounts()
            const defaultAccount = account[0];
            console.log("defaultAccount: " + defaultAccount)
            if(defaultAccount === null){
              if (!this.window.localStorage.getItem('enabled')) {
                this.window.localStorage.setItem('enabled', 'true');
                this.document.location.reload();
                return;
              }
              reject({message: '请解锁 metamask 并刷新本页面'})
            }
            results = {
              web3Instance: web3,
              netIdName,
              netId,
              injectedWeb3: true,
              defaultAccount,
              trustApiName,
              explorerUrl,
              contractAddress
            }
            resolve(results);
          } catch (error) {
            console.log(error)
            reject(error);
          }
        } else {
          reject('　以太坊钱包不可获取　')
        }
      });
    });

export default getWeb3
